package com.borrower.dao;



import com.borrower.dto.BorrowInfoBean;
import com.pojo.BookDamageBean;
import com.pojo.BorrowerBean;
import com.pojo.QueryBookBean;
import com.pojo.UserSendMsgBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BorrowerDaoImpl implements BorrowerDao {

    @Override
    public boolean Regis(String sql, Connection conn, BorrowerBean bor) throws SQLException {
        PreparedStatement pstm = null;
        boolean bool = false;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bor.getUserName());
            pstm.setString(2, bor.getBorPwd());
            pstm.setString(3, bor.getEmail());
            pstm.setString(4, bor.getIdCard());
            pstm.setString(5, bor.getPhone());
            int ins = pstm.executeUpdate();
            if (ins == 1) {
                bool = true;
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
        }
        return bool;
    }

    //判断注册时用户名是否存在
    @Override
    public boolean RegisName(String sql, Connection conn, BorrowerBean bor) throws SQLException {

        PreparedStatement pstm = null;
        boolean bool = false;
        ResultSet rs = null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bor.getUserName());
            rs= pstm.executeQuery();
            bool = rs.next();
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs !=null){
                rs.close();
            }
        }
        return bool;
    }

    @Override
    public boolean Login(String sql, Connection conn, BorrowerBean bor) throws SQLException {
        PreparedStatement pstm = null;
        boolean bool = false;
        ResultSet rs = null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bor.getUserName());
            pstm.setString(2, bor.getBorPwd());
            rs= pstm.executeQuery();
            bool = rs.next();
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs !=null){
                rs.close();
            }
        }
        return bool;
    }

    @Override
    public List<BorrowerBean> getBorInfoByIdCard(BorrowerBean bb, Connection conn, String sql) throws SQLException {
        List<BorrowerBean> list = new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        BorrowerBean borObj = new BorrowerBean();

        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bb.getIdCard());
            rs = pstm.executeQuery();
            if(rs.next()) {
                borObj.setUserName(rs.getString("userName"));
                borObj.setEmail(rs.getString("email"));
                borObj.setIdCard(rs.getString("idCard"));
                borObj.setPhone(rs.getString("phone"));
                borObj.setBalance(rs.getDouble("balance"));
                borObj.setCreateDate(rs.getString("createDate"));
                list.add(borObj);
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return list;
    }


    @Override
    public List<QueryBookBean> getBookInfoByName(String bookName, Connection conn, String sql)throws SQLException {
        List<QueryBookBean> list = new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        QueryBookBean borObj = new QueryBookBean();

        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bookName);
            rs = pstm.executeQuery();
            if(rs.next()) {
                borObj.setBorrower(rs.getString("username"));
                borObj.setBookName(rs.getString("bookname"));
                borObj.setBorDate(rs.getDate("bordate"));
                borObj.setRepayDate(rs.getString("repaydate"));
                borObj.setPress(rs.getString("press"));
                borObj.setPrice(rs.getDouble("price"));
                list.add(borObj);
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return list;
    }

    //显示全部借书信息列表
    @Override
    public List<BorrowInfoBean> BorrowInfo(String sql,Connection conn,String loginName) throws SQLException {
        List<BorrowInfoBean> list= new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        BorrowInfoBean alb = null;
        try {
            pstm =conn.prepareStatement(sql);
            pstm.setString(1,loginName);

            rs = pstm.executeQuery();
            while (rs.next()){
                alb = new BorrowInfoBean();
                alb.setUserName(rs.getString("username"));
                alb.setBookid(rs.getString("bookid"));
                alb.setBookName(rs.getString("bookname"));
                alb.setBorDate(rs.getString("bordate"));
                alb.setRepayDate(rs.getString("repaydate"));
                alb.setPrice(rs.getDouble("price"));
                alb.setState(rs.getString("state"));
                list.add(alb);
            }
        }finally {
            if (pstm !=null){
                pstm.close();
            }
            if (rs !=null){
                rs.close();
            }
        }
        return list;
    }

    public boolean SendMsgDao(String sql, Connection conn, UserSendMsgBean userSendMsgBean) throws SQLException{
        PreparedStatement pstm = null;
        boolean bool = false;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setLong(1, userSendMsgBean.getUserId());
            pstm.setString(2, userSendMsgBean.getMsg());
            pstm.setString(3, userSendMsgBean.getMsgSendTime());
            int ins = pstm.executeUpdate();
            if (ins == 1) {
                bool = true;
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
        }
        return bool;
    }


    /**
     * 用户注销界面显示用户个人信息
     * @param sql
     * @param conn
     * @param loginName
     * @return
     * @throws SQLException
     */
    @Override
    public List<BorrowerBean> UserInfo(String sql, Connection conn, String loginName) throws SQLException {
        List<BorrowerBean> list= new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        BorrowerBean alb = null;
        try {
            pstm =conn.prepareStatement(sql);
            pstm.setString(1,loginName);

            rs = pstm.executeQuery();
            while (rs.next()){
                alb = new BorrowerBean();
                alb.setUserId(rs.getLong("userid"));
                alb.setCreateDate(rs.getString("createdate"));
                alb.setUserName(rs.getString("username"));
                alb.setEmail(rs.getString("email"));
                alb.setIdCard(rs.getString("idcard"));
                alb.setPhone(rs.getString("phone"));
                alb.setBalance(rs.getDouble("balance"));
                alb.setIsCancel(rs.getString("iscancel"));
                alb.setIsLogin(rs.getString("islogin"));
                alb.setIsCancel(rs.getString("iscancel"));
                alb.setBoringVolume(rs.getLong("borrowingvolume"));
                list.add(alb);
            }
        }finally {
            if (pstm !=null){
                pstm.close();
            }
            if (rs !=null){
                rs.close();
            }
        }
        return list;
    }

    /**
     * 将用户注销状态设置为1
     * @param sql
     * @param conn
     * @param bor
     * @param loginName
     * @return
     * @throws SQLException
     */
    @Override
    public boolean Cancel(String sql, Connection conn,BorrowerBean bor, String loginName) throws SQLException {
        PreparedStatement pstm = null;
        boolean bool = false;
        //ResultSet rs = null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, bor.getIsCancel());
            pstm.setString(2, loginName);
            // rs= pstm.executeQuery();
            int len = pstm.executeUpdate();
            if (len >0){
                bool = true;
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
        }
        return bool;
    }

    /**
     * 提交书籍报损信息
     * @param sql
     * @param conn
     * @param bor
     * @return
     * @throws SQLException
     */
    @Override
    public boolean BookDamageInfo(String sql, Connection conn, BookDamageBean bor) throws SQLException {
        PreparedStatement pstm = null;
        boolean bool = false;
        try {
            pstm = conn.prepareStatement(sql);
//            pstm.setLong(1, bor.getBookDamageMsgId());
            pstm.setLong(1, bor.getUserId());
            pstm.setLong(2, bor.getBookId());
            pstm.setString(3, bor.getBookDamageMsg());
            pstm.setString(4, bor.getDamagedegree());
            int ins = pstm.executeUpdate();
            if (ins == 1) {
                bool = true;
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
        }
        return bool;
    }


}
